package com.ccl.iot.huiju;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.ccl.iot.R;
import com.ccl.iot.dvr.utils.LogUtil;
import com.ccl.iot.dvr.utils.ToastUtil;
import com.ccl.iot.huiju.adapter.EditAdapter;
import com.ccl.iot.huiju.util.HuiJuUtil;
import com.ccl.iot.publics.UIUtils;

import java.util.ArrayList;


/**
 * 类说明：添加设备或情景模式
 * 作者：HD on 2017/10/16 17:33
 * 邮箱：hehandong@qq.com
 */
public class EditItemFragment extends BaseFragment {

    //-----------------UI--------------------
    private GridView mGridView;
    public EditAdapter mAdapter;
    private EditItemFragment mEditItemFragment;

    //-----------------Data--------------------

    public final static String TAG = "EditItemFragment";

    private ArrayList<IOTHuiJu> mIOTHuiJuList;

    private ArrayList<IOTHuiJu> mDataList;
    private RelativeLayout mLeftLayout;
    private RelativeLayout mRightLayout;
    private Button mDeleteBtn;
    private Button mLogoutBtn;

    private int mEditPage = 0;

    private float mPoint2DownX;

    private float mPoint2MoveX;
    private ImageView mLogoIv;


    public interface OnEditItemFragmentListener {
        void onEditPositionSelected(IOTHuiJu iotHuiJu, int page, int position);

        void onEditItemFragmentDestroy();

        void onEditRemoveIOTHuiJu(IOTHuiJu iotHuiJu);
    }


    public EditItemFragment() {
        // Required empty public constructor
    }


    public static EditItemFragment newInstance() {
        EditItemFragment fragment = new EditItemFragment();
        return fragment;
    }

    @Override
    protected View setContentView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_add_item2, container, false);
    }

    @Override
    protected void initView(View view) {

        mIOTHuiJuList = new ArrayList<>();

        mDataList = new ArrayList<>();

        mGridView = (GridView) view.findViewById(R.id.gridView_huiju);

        mEditItemFragment = this;

        mAdapter = new EditAdapter(mContext, mDataList, mEditPage);

        mGridView.setAdapter(mAdapter);


        mLeftLayout = (RelativeLayout) view.findViewById(R.id.layout_left);

        mRightLayout = (RelativeLayout) view.findViewById(R.id.layout_right);

        mDeleteBtn = (Button) view.findViewById(R.id.btn_delete);
        mLogoutBtn = (Button) view.findViewById(R.id.btn_logout);

        mLogoIv = (ImageView) view.findViewById(R.id.iv_ccl_logo);


    }


    @Override
    protected void initListener() {

        mLeftLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchPage(mEditPage, false);

            }
        });

        mRightLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchPage(mEditPage, true);
            }
        });

        mLogoIv.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String versionName = HuiJuUtil.getVersionName(mContext);
                String versionCode = HuiJuUtil.getVersionCode(mContext);
                ToastUtil.showLongToast("当前版本：" + versionName +":" +versionCode);
                return false;
            }
        });

        mDeleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getActivity() instanceof OnEditItemFragmentListener) {
                    ((OnEditItemFragmentListener) getActivity()).onEditItemFragmentDestroy();
                }
            }
        });

        mLogoutBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getActivity() instanceof MainActivity) {
                    ((MainActivity) getActivity()).logoutUser();
                }
            }
        });


        if (getActivity() instanceof OnEditItemFragmentListener) {
            mAdapter.setOnEditItemFragmentListener((OnEditItemFragmentListener) getActivity());
        }

        mGridView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {


                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        mPoint2DownX = event.getX();
                        LogUtil.i(TAG, "ACTION_DOWN");

                        break;
                    case MotionEvent.ACTION_MOVE:

                        break;
                    case MotionEvent.ACTION_CANCEL:
//                            LogUtil.i(TAG,"ACTION_CANCEL");
                        break;
                    case MotionEvent.ACTION_UP:
                        LogUtil.i(TAG, "ACTION_UP");

                        mPoint2MoveX = event.getX();
                        float diff = mPoint2MoveX - mPoint2DownX;
                        LogUtil.i(TAG, "ACTION_MOVE--" + "mPoint2DownY:" + mPoint2DownX
                                + "--point2MoveX:" + mPoint2MoveX + "--diff:" + diff);

                        if (diff > 5) {
                            LogUtil.i(TAG, "diff > 10");
                            switchPage(mEditPage, false);
                        } else if (diff < -5) {
                            LogUtil.i(TAG, "diff < -10");
                            switchPage(mEditPage, true);
                        }

                        break;
                    default:
                        break;
                }

                return true;
            }
        });

    }


    @Override
    protected void initData() {

    }


    @Override
    protected void onShow() {

    }

    @Override
    protected void onHide() {

    }


    //-----------------------业务逻辑----------------

    public void refreshUI(ArrayList<IOTHuiJu> iotHuiJuList, int page) {
        mIOTHuiJuList = iotHuiJuList;
        mEditPage = page;
        HuiJuUtil.initAllPositionData(mIOTHuiJuList);

        UIUtils.postTaskSafely(new Runnable() {
            @Override
            public void run() {
                switchPage(mEditPage);
            }
        });

    }

    public void switchPage(int pageIndex) {


        if (mAdapter != null && mDataList != null && mIOTHuiJuList != null) {
            mDataList.clear();
            int size = mIOTHuiJuList.size();
            int pageCount = size / 9;

            if (size > 0 && pageIndex >= 0 && pageIndex <= pageCount) {
                int startIndex = 9 * pageIndex;
                int endIndex = 9 * (pageIndex + 1);

                if (startIndex < size) {
                    if (endIndex > size) {
                        endIndex = size;
                    }
                    for (int i = startIndex; i < endIndex; i++) {
                        IOTHuiJu iotHuiJu = mIOTHuiJuList.get(i);
                        if (!mDataList.contains(iotHuiJu)) {
                            mDataList.add(iotHuiJu);
                        }
                    }
                }


                if (pageIndex == 0){
                    mLeftLayout.setVisibility(View.INVISIBLE);
                }else {
                    mLeftLayout.setVisibility(View.VISIBLE);
                }

                if (mDataList.size() < 9){
                    mRightLayout.setVisibility(View.INVISIBLE);
                }else {
                    mRightLayout.setVisibility(View.VISIBLE);
                }

                mEditPage = pageIndex;
                mAdapter.notifyDataSetChanged(mDataList, mEditPage);

            }
        }

    }

    public void switchPage(int pageIndex, boolean isAdd) {
        if (isAdd) {
            pageIndex++;
            switchPage(pageIndex);
        } else {
            pageIndex--;
            switchPage(pageIndex);
        }
    }

    public EditAdapter getAdapter(){
        return mAdapter;
    }


}
